package com.ddz.implementation;

import java.util.Stack;

import com.ddz.interfaces.State;

public class GameStateManager {

	Stack<State> mGameStates;
	static volatile GameStateManager mInstance = null;

	private GameStateManager() {
		mGameStates = new Stack<State>();
	}

	public State getTopState() {
		return mGameStates.firstElement();
	}

	static public GameStateManager getInstance() {
		if (mInstance == null)
			mInstance = new GameStateManager();
		return mInstance;
	}

	public void pushState(State newState) {
		if (newState != null && !mGameStates.contains(newState))
		{
			if (mGameStates.size() != 0) {
				mGameStates.lastElement().onPause();
			}
			newState.onInit();
			newState.onResume();
			mGameStates.push(newState);
		}
	}

	public void popState() {
		mGameStates.lastElement().dispose();
		mGameStates.pop();
	}
}
